<?php

namespace app\services;

use plugin\admin\app\model\UserSearchHistory;

class KeywordService
{
    public static function list($type = UserSearchHistory::TYPE_MUSIC)
    {
        return UserSearchHistory::where('user_id', get_user_id())->where('type', $type)->orderByDesc('created_time')->get()->toArray();
    }

    public static function clear($type = UserSearchHistory::TYPE_MUSIC)
    {
        UserSearchHistory::where('user_id', get_user_id())->where('type', $type)->delete();
    }

    public static function add($keyword, $type = UserSearchHistory::TYPE_MUSIC)
    {
        if (UserSearchHistory::where('user_id', get_user_id())->where('type', $type)->where('keyword', $keyword)->count() == 0) {
            UserSearchHistory::insert([
                'type' => $type,
                'user_id' => get_user_id(),
                'keyword' => $keyword,
                'created_time' => date('Y-m-d H:i:s')
            ]);
        }
    }
}